package com.leo.ibatis.demo.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.leo.ibatis.demo.criteria.AccountCriteria;
import com.leo.ibatis.demo.domain.Account;
import com.leo.ibatis.demo.service.AccountService;

public class AccountController extends MultiActionController {

	private AccountService accountService;

	public void setAccountService(AccountService accountService) {
		this.accountService = accountService;
	}

	public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		AccountCriteria accountCriteria = new AccountCriteria();
		bind(request, accountCriteria);
		
		List<Account> accountList = accountService.listOnPage(accountCriteria);
		
		ModelAndView mav = new ModelAndView("account/list");
		mav.addObject("accountList", accountList);
		mav.addObject("accountCriteria", accountCriteria);
		
		return mav;
	}

	public ModelAndView detail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		AccountCriteria accountCriteria = new AccountCriteria();
		bind(request, accountCriteria);
			
		String userid = ServletRequestUtils.getRequiredStringParameter(request, "userid");

		Account account = accountService.select(userid);
		
		ModelAndView mav = new ModelAndView("account/detail");
		mav.addObject("account", account);
		mav.addObject("accountCriteria", accountCriteria);
		
		return mav;
	}

	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String userid = ServletRequestUtils.getRequiredStringParameter(request, "userid");

		accountService.delete(userid);
		
		return new ModelAndView("redirect:account.do");
	}

}
